package com.wn.icloud.auth.client.configuration;

import com.wn.icloud.auth.client.config.ServiceAuthConfig;
import com.wn.icloud.auth.client.config.UserAuthConfig;
import com.wn.icloud.auth.client.interceptor.ServiceAuthRestInterceptor;
import com.wn.icloud.auth.client.interceptor.UserAuthRestInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * Created by leeyang on 2017/9/15.
 */
@Configuration
@ConditionalOnProperty(value = "auth.interceptor", havingValue = "true")
public class AuthInterceptorConfiguration implements WebMvcConfigurer {

    @Autowired
    private ServiceAuthConfig serviceAuthConfig;

    @Autowired
    private UserAuthConfig userAuthConfig;

    @Value("${auth.disable-server-auth:true}")
    private Boolean disableServerAuth;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {

        if(!disableServerAuth){
            registry.addInterceptor(getServiceAuthRestInterceptor())
                    .addPathPatterns(serviceAuthConfig.getIncludeUrlPatter())
                    .excludePathPatterns(serviceAuthConfig.getExcludeUrlPatter());
        }
        registry.addInterceptor(getUserAuthRestInterceptor())
                .addPathPatterns(userAuthConfig.getIncludeUrlPatter())
                .excludePathPatterns(userAuthConfig.getExcludeUrlPatter());
    }

    @Bean
    ServiceAuthRestInterceptor getServiceAuthRestInterceptor() {
        return new ServiceAuthRestInterceptor();
    }

    @Bean
    UserAuthRestInterceptor getUserAuthRestInterceptor() {
        return new UserAuthRestInterceptor();
    }

//    @Bean
//    ServiceAuthConfig getServiceAuthConfig() {
//        return new ServiceAuthConfig();
//    }
//
//    @Bean
//    UserAuthConfig getUserAuthConfig() {
//        return new UserAuthConfig();
//    }


}
